#!/bin/bash
#Author:lixinglilove@hotmail.com
#CreateTime:20200710

PyDown="https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz"
FileName=$(echo $PyDown | awk -F "/" '{print $7}')
DirName=$(echo $FileName |awk -F ".tgz" '{print $1}')
echo "当前Python版本为" $(python -V)
echo "升级Python版本为：2.7.18"
echo "下载安装包,内网环境请离线下载安装包\n下载地址为：${PyDown}"
echo "下载目录为/tmp"
sleep 1

function network()
{
    #超时时间
    local timeout=1
    #目标网站
    local target=www.baidu.com
    #获取响应状态码
    local ret_code=`curl -I -s --connect-timeout ${timeout} ${target} -w %{http_code} | tail -n1`
    if [ "x$ret_code" = "x200" ]; then
        #网络畅通
        return 1
    else
        #网络不畅通
        return 0
    fi
    return 0
}
network
if [ $? -eq 0 ];then
        echo "网络不畅通，请检查网络设置！"
        exit 1
fi
which wget
if [ $? -eq 1 ];then
	yum -y install wget
fi
cd /tmp && wget $PyDown
echo "解压安装包"
tar zxvf $FileName
cd $DirName
./configure --prefix=/usr/local && make && make altinstall

if [ $? -eq 1 ];then
	echo "编译异常，请检查gcc是否安装"
	yum -y install gcc
	./configure --prefix=/usr/local && make && make altinstall
else
	echo "备份升级Python"
fi

mv /usr/bin/python /usr/bin/python2.7.5
ln -s /usr/local/bin/python2.7 /usr/bin/python
sed -i 's/python/python2.7.5/g'  /usr/bin/yum
sed -i 's/python/python2.7.5/g'  /usr/libexec/urlgrabber-ext-down
echo "当前Pyton版本为" $(python -V)
rm -rf $DirName
rm -rf $FileName